# -*- coding: utf-8 -*-
import scrapy
import json
from dy.items import DyItem

class DouyuSpider(scrapy.Spider):
    name = 'douyu'
    allowed_domains = ['douyucdn.cn']
    baseUrl = 'https://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset='
    offset = 0
    start_urls = [baseUrl + str(offset)]

    def parse(self, response):
        data1 = json.loads(response.body)
        datas = data1['data']
        for d in datas:
            vertical_src = d['vertical_src']
            room_name = d['room_name']
            print(vertical_src,room_name,self.offset)

            item = DyItem()
            item['vertical_src'] = vertical_src
            item['room_name'] = room_name
            yield item

        if len(datas) != 0 :
            self.offset = self.offset + 1
            url = self.baseUrl + str(self.offset)
            yield scrapy.Request(url,callback=self.parse)
            
        
